package cn.xgj.sfzy.controller;

import cn.xgj.sfzy.entity.ChargeType;
import cn.xgj.sfzy.service.ChargeTypeService;
import cn.xgj.sfzy.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 张卫姣
 * @Date 2024年10月24日 11:50
 * @Version 1.0
 */
@RestController
@RequestMapping("/chargeType")
public class ChargeTypeController {

    @Autowired
    private ChargeTypeService chargeTypeService;

    @PostMapping("/addChargeType")
    public Result addChargeType(@RequestBody ChargeType chargeType){
        Integer num = chargeTypeService.add(chargeType);
        if (num!=0){
            return Result.success("添加成功！");
        }else{
            return Result.error("添加失败！");
        }

    }

    @DeleteMapping("/removeChargeType/{chargeTypeId}")
    public Result removeChargeType(@PathVariable("chargeTypeId") Integer chargeTypeId){
        Integer num = chargeTypeService.remove(chargeTypeId);
        if (num!=0){
            return Result.success("删除成功！");
        }else{
            return Result.error("删除失败！");
        }

    }

    @PutMapping("/editChargeType")
    public Result editChargeType(@RequestBody ChargeType chargeType){
        Integer num = chargeTypeService.edit(chargeType);
        if (num!=0){
            return Result.success("修改成功！");
        }else{
            return Result.error("修改失败！");
        }

    }

    @GetMapping("/findAll")
    public Result findAll(ChargeType chargeType){
        List<ChargeType> chargeTypes = chargeTypeService.findAll(chargeType);
        return Result.success(chargeTypes);
    }

    @GetMapping("/findById/{chargeTypeId}")
    public Result findById(@PathVariable("chargeTypeId") Integer chargeTypeId){
        ChargeType chargeType = chargeTypeService.findById(chargeTypeId);
        return Result.success(chargeType);
    }

    @GetMapping("/findByPage")
    public Result findByPage(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "5")Integer pageSize,ChargeType chargeType){

        PageInfo<ChargeType> pageInfo = chargeTypeService.findByPage(pageNum, pageSize, chargeType);
        return Result.success(pageInfo);

    }


}
